package com.geeguo.ebuilder.client.business.system.excelimportfailitem.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geeguo.ebuilder.client.base.database.annotation.TDS;
import com.geeguo.ebuilder.client.base.tenant.model.DataContext;
import com.geeguo.ebuilder.client.business.system.excelimportfailitem.mapper.ExcelImportFailItemMapper;
import com.geeguo.ebuilder.client.business.system.excelimportfailitem.model.ExcelImportFailItemConverter;
import com.geeguo.ebuilder.client.business.system.excelimportfailitem.model.ExcelImportFailItemEntity;
import com.geeguo.ebuilder.client.business.system.excelimportfailitem.model.ExcelImportFailItemQuery;
import com.geeguo.ebuilder.client.business.system.excelimportfailitem.model.ExcelImportFailItemVO;
import com.geeguo.ebuilder.client.business.system.excelimportfailitem.service.ExcelImportFailItemService;
import com.geeguo.ebuilder.core.common.model.vo.PageListVO;
import com.geeguo.ebuilder.core.common.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ExcelImportFailItemServiceImpl extends ServiceImpl<ExcelImportFailItemMapper, ExcelImportFailItemEntity> implements ExcelImportFailItemService {

    @Autowired
    private ExcelImportFailItemMapper excelImportFailItemMapper;

    @Override
    public PageListVO<ExcelImportFailItemVO> list(ExcelImportFailItemQuery query, DataContext dataContext) {
        Map<String, Object> params = new HashMap<>();
        params.put("tenantCode", dataContext.getTenantCode());
        IPage<ExcelImportFailItemEntity> page = excelImportFailItemMapper.list(new Page<>(query.getPageNo(), query.getPageSize()), params);
        List<ExcelImportFailItemVO> result = new ArrayList<>();
        if (page.getRecords() != null && !page.getRecords().isEmpty()) {
            for (ExcelImportFailItemEntity item : page.getRecords()) {
                result.add(ExcelImportFailItemConverter.INSTANCE.convert2VO(item));
            }
        }
        return PageUtils.getPageListVO(page, result);
    }

    @TDS
    @Override
    public ExcelImportFailItemVO get(String id, DataContext dataContext) {
        ExcelImportFailItemEntity result = excelImportFailItemMapper.selectById(id);
        if (result != null) {
            return ExcelImportFailItemConverter.INSTANCE.convert2VO(result);
        }
        return null;
    }

    @TDS
    @Override
    public boolean save(ExcelImportFailItemEntity entity, DataContext dataContext) {
        return excelImportFailItemMapper.insert(entity) > 0;
    }

    @TDS
    @Override
    @Transactional
    public boolean batchSave(List<ExcelImportFailItemEntity> list, DataContext dataContext) {
        return super.saveBatch(list);
    }

    @TDS
    @Override
    public boolean remove(String id, DataContext dataContext) {
        return excelImportFailItemMapper.deleteById(id) == 1;
    }

    @TDS
    @Override
    public boolean removeByImportId(String importId, DataContext dataContext) {
        return excelImportFailItemMapper.removeByImportId(importId) >= 0;
    }
}
